﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IAPM_GPC_dll;
using System.Globalization;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ClassConnect.seConnecter();
            ClassConnect.Chargement_DataGrid(dataGridView1, "Select * from Client", "lesClient");
            ClassConnect.Chargement_ComboBox(cmbVisiteCli, "Select id,idClipro,concat(nom,' ', prenom) as 'nomPrenom' from Client c,Client_Prospect cp  where c.id = cp.idCliPro ", "CliProsp", "idCliPro", "nomPrenom");

            ClassConnect.Chargement_DataGrid(DgVisite, "Select * from Visites", "lesVisite");


            ClassConnect.Chargement_DataGrid(dgCommande, "Select * from Commande", "lesCommande");

            ClassConnect.Chargement_ComboBox(CbClient, "Select * from Client", "lesClientCommande", "id", "nom");


            ClassConnect.Chargement_ComboBox(cmbVisiteResp, "Select idRepresentant, concat(nom,' ',prenom) as 'nomPrenom'from Representant", "Representant", "idRepresentant", "nomPrenom");


        }

        #region Onglet Client

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            TextBoxNumero.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            textBoxAdressClient.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBoxCPClient.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            textBoxVilleClient.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            textBoxNomClient.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            textBoxPrenomClient.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            textBoxEmail.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            string sexe = dataGridView1.CurrentRow.Cells[7].Value.ToString();
            if (sexe == "1")
            {
                radioHomme.Checked = true;
                radioFemme.Checked = false;
            }
            else
            {
                radioHomme.Checked = false;
                radioFemme.Checked = true;
            }
        }

        private void BtnModifier_Click(object sender, EventArgs e)
        {
            if (BtnModifier.Text == "Modifier")
            {

                btnAjouterClient.Visible = false;

                BtnSupprimer.Text = "Annuler";
                BtnModifier.Text = "Valider";

            }
            else if (BtnModifier.Text == "Valider")
            {
                string i = TextBoxNumero.Text;
                string n = textBoxAdressClient.Text;
                string p = textBoxCPClient.Text;
                string a = textBoxVilleClient.Text;
                string cp = textBoxNomClient.Text;
                string v = textBoxPrenomClient.Text;
                string em = textBoxEmail.Text;
                string s;
                if (radioFemme.Checked == true)
                {
                    s = "0";
                }
                else
                {
                    s = "1";
                }
                ClassConnect.Executer_Requete_Action("UPDATE client SET adRueClient = '" + n + "' , adCpClient = '" + p + "' , adVilleClient = '" + a + "' , nom = '" + cp + "' , prenom = '" + v + "' , '" + em + "' , '" + s + "' WHERE id  = " + i + " ");

                MessageBox.Show("Modification OK", "modification", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ClassConnect.ViderVue("lesClient");
                ClassConnect.Chargement_DataGrid(dataGridView1, "Select * from Client", "lesClient");
                BtnSupprimer.Text = "Suprimer";
                BtnModifier.Text = "Modifier";
                btnAjouterClient.Visible = true;
            }
        }

        private void btnAjouterClient_Click(object sender, EventArgs e)
        {
            if (btnAjouterClient.Text == "Ajouter")
            {

                ClassConnect.ViderVue("cbCli");
                BtnModifier.Visible = false;
                BtnSupprimer.Text = "Annuler";
                btnAjouterClient.Text = "Valider";
                textBoxNomClient.Text = "";
                textBoxPrenomClient.Text = "";
                textBoxAdressClient.Text = "";
                textBoxCPClient.Text = "";
                textBoxVilleClient.Text = "";
                textBoxEmail.Text = "";
                TextBoxNumero.Text = ClassConnect.Generer_Cle_Primaire("id", "client").ToString();

            }
            else
            {
                if ((TextBoxNumero.Text != "") || (textBoxNomClient.Text != "") || (textBoxPrenomClient.Text != "") || (textBoxAdressClient.Text != "") || (textBoxCPClient.Text != "") || (textBoxVilleClient.Text != "") || (textBoxEmail.Text != ""))
                {
                    string id = TextBoxNumero.Text;
                    string nom = textBoxNomClient.Text;
                    string prenom = textBoxPrenomClient.Text;
                    string addresse = textBoxAdressClient.Text;
                    string cp = textBoxCPClient.Text;
                    string ville = textBoxVilleClient.Text;
                    string mail = textBoxEmail.Text;
                    string sexe = "";
                    if (radioHomme.Checked == true)
                    {
                        sexe = "1";
                    }
                    if (radioFemme.Checked == true)
                    {
                        sexe = "0";
                    }


                    try
                    {
                        ClassConnect.Executer_Requete_Action("Insert into client Values ( " + id + ", '" + addresse + "',  " + cp + " , '" + ville + "' ,'" + nom + "', '" + prenom + "', '" + mail + "' ,'" + sexe + "')");
                        MessageBox.Show("Client Ajoutée...!", "Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    catch (Exception)
                    {

                        MessageBox.Show("Un compte administrateur ne peu pas etre un client");
                    }

                    ClassConnect.ViderVue("lesClient");
                    btnAjouterClient.Text = "Ajouter";

                    ClassConnect.Chargement_DataGrid(dataGridView1, "Select * from client", "lesClient");



                    //ChargementsGraphiques.TexteBoxClient(tbIDClient, tbNomClient, tbPrenomClient, tbAdresseClient, tbCPClient, tbVilleClient, dgvClient);
                }
                else
                {
                    MessageBox.Show("Renseigner tous les champs SVP...!", "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


                BtnModifier.Visible = true;
                BtnSupprimer.Text = "Supprimer";
                btnAjouterClient.Text = "Ajouter";

            }
        }

        private void BtnSupprimer_Click(object sender, EventArgs e)
        {
            if (BtnSupprimer.Text == "Annuler")
            {
                if (btnAjouterClient.Text == "Valider")
                {

                    BtnModifier.Visible = true;
                    btnAjouterClient.Text = "Ajouter";
                    ClassConnect.ViderVue("lesClient");
                    ClassConnect.Chargement_DataGrid(dataGridView1, "Select * from Client", "lesClient");
                    BtnSupprimer.Text = "Supprimer";

                }
                if (BtnModifier.Text == "Valider")
                {


                    BtnModifier.Visible = true;
                    BtnModifier.Text = "Modifier";
                    btnAjouterClient.Visible = true;
                }
            }
            else
            {

                string i = TextBoxNumero.Text;
                string n = textBoxNomClient.Text;
                string p = textBoxPrenomClient.Text;

                if (MessageBox.Show("Etes vous sur de vouloir suprimer le client " + n + " " + p + " ?", "ATTENTION !", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ClassConnect.Executer_Requete_Action("DELETE FROM client WHERE (id ='" + i + "') ");
                    MessageBox.Show("le client a été Suprimé...!", "Supression", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ClassConnect.ViderVue("LesClient");
                    ClassConnect.Chargement_DataGrid(dataGridView1, "Select * from Client", "lesClient");
                }
            }
        }

        #endregion


        #region Onglet Commande

        public void btnAjoutCom_Click(object sender, EventArgs e)
        {
            if (btnAjoutCom.Text == "Ajouter")
            {

                ClassConnect.ViderVue("cbClient");

                btnSupprimerCom.Text = "Annuler";
                btnAjoutCom.Text = "Valider";
                txtNumero.Text = "";
                TxtDate.Text = "";

            }
            else
            {
                if ((CbClient.Text != "") || (txtNumero.Text != "") || (TxtDate.Text != ""))
                {
                    string i = CbClient.ValueMember;
                    string n = txtNumero.Text;
                    string d = TxtDate.Text;

                    try
                    {
                        ClassConnect.Executer_Requete_Action("Insert into Client Values ( " + i + ", '" + n + "', '" + d + "')");

                        MessageBox.Show("Commande Ajoutée...!", "Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    catch (Exception)
                    {

                        MessageBox.Show("Un compte administrateur ne peu pas etre un client");
                    }

                    ClassConnect.ViderVue("lesClientCommande");
                    btnAjoutCom.Text = "Ajouter";

                    ClassConnect.Chargement_DataGrid(dgCommande, "SELECT * FROM Commande", "lesClientCommande");

                    btnAjoutCom.Visible = false;



                }
                else
                {
                    MessageBox.Show("Renseigner tous les champs SVP...!", "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnSupprimerCom_Click(object sender, EventArgs e)
        {
            if (btnSupprimerCom.Text == "Supprimer")
            {
                string i = txtNumero.Text;
                if (MessageBox.Show("Etes vous sur de vouloir suprimer la commande " + i + " ?", "ATTENTION !", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ClassConnect.Executer_Requete_Action("DELETE FROM commande WHERE idcommande = " + i + " ");
                    MessageBox.Show("la commande a été Suprimé...!", "Supression", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ClassConnect.ViderVue("lesCommande");
                    ClassConnect.Chargement_DataGrid(dgCommande, "Select * from Commande", "lesCommande");
                }
            }
        }

        #endregion


        #region Onglet Visite

        private void txtVisiteDate_Enter(object sender, EventArgs e)
        {
            if (txtVisiteDate.Text == "jj/mm/aaaa")
            {
                txtVisiteDate.ForeColor = Color.Black;
                txtVisiteDate.Text = "";
            }
        }

        private void txtVisiteDate_TextChanged(object sender, EventArgs e)
        {
            DateTime dateValue;
            string date = txtVisiteDate.Text;
            if (!DateTime.TryParseExact(date, "dd/MM/aaaa", new CultureInfo("fr-FR"), DateTimeStyles.None, out dateValue))
            {
                erp.SetError(txtVisiteDate, "La date saisie ne correspond pas au format demandé!");
            }
            else
            {
                erp.Clear();
            }

        }

        private void refreshOngletCli()
        {
            ClassConnect.Chargement_DataGrid(DgVisite, "Select * from Visites", "lesVisite");
        }

        private void btnVisiteAjt_Click(object sender, EventArgs e)
        {
            if (txtVisiteDate.Text != "jj/mm/aaaa")
            {
                object idRep = cmbVisiteResp.SelectedValue;
                object idCli = cmbVisiteCli.SelectedValue;
                string date = txtVisiteDate.Text;
                ClassConnect.Executer_Requete_Action("Insert into Visites Values(" + idCli + "," + idRep + ",'" + date + "')");
                MessageBox.Show("Visite enregistrée!!!");
                refreshOngletCli();
            }
        }

        private void DgVisite_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            cmbVisiteCli.SelectedValue = DgVisite.CurrentRow.Cells[0].Value;
            cmbVisiteResp.SelectedValue = DgVisite.CurrentRow.Cells[1].Value;
            txtVisiteDate.Text = DgVisite.CurrentRow.Cells[2].Value.ToString();
            txtVisiteDate.ForeColor = Color.Black;
        }

        private void btnVisiteSupr_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Voulez vous vraiment supprimer cette visite de la BDD?", "ATTENTION!", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                object idCli = cmbVisiteCli.SelectedValue;
                object idRep = cmbVisiteResp.SelectedValue;
                string date = txtVisiteDate.Text;
                ClassConnect.Executer_Requete_Action("DELETE FROM Visites where idCli =" + idCli + " and idRep =" + idRep + " and dateVisite = '" + date + "'");
                refreshOngletCli();
            }
        }

        private void tabPage4_Paint(object sender, PaintEventArgs e)
        {
            refreshOngletCli();
        }

        #endregion
    }
}